package com.sgj.lock;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Map线程不安全问题解决方案
 * @Author: Guoji Shen
 * @Date: 2021/8/5 13:42
 */
public class MapThreadDemo {

    public static void main(String[] args) {

        // 创建一个HashMap
        // Map<String, String> map = new HashMap<>();

        // juc提供的解决方案  ConcurrentHashMap
        Map<String, String> map = new ConcurrentHashMap<>();

        for (int i = 0; i < 30; i++) {
            String key = String.valueOf(i);
            new Thread(() -> {
                // 向集合添加内容
                map.put(key, UUID.randomUUID().toString().substring(0, 8));
                System.out.println(map);
            }, String.valueOf(i)).start();
        }

    }
}